package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DuplicateCheck
 */
//@WebServlet(description = "確認是否存在同名的測試案例", urlPatterns = { "/DuplicateCheck" })
public class DuplicateCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DuplicateCheck() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tcID = request.getParameter("tcID");				//測試案例ID
		
		String strSQL = "SELECT t1.tcID FROM tms_tcidmapping t1 WHERE t1.tcID='" + tcID + "'";
		
		Connection conn = null;
		Widgets wid = new Widgets();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<TestCase> testCases = new ArrayList<TestCase>();
		int tt = 0;
		
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while (rs.next()) {
				tt++;
				TestCase testCase = new TestCase();
				testCase.setTcID(rs.getString("tcID"));
				testCases.add(testCase);
			}
			
			response.getWriter().write(String.valueOf(tt));
		} catch (SQLException e) {
			wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}
	}
}